package com.xw.comm.service.map.impl;

import com.xw.comm.entity.map.BackgroundTreeNode;
import com.xw.comm.entity.map.BaseMapObject;
import com.xw.comm.service.map.BackgroundMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiongwei_vendor
 * @date 2021/10/12 17:28
 */
@Slf4j
@Service
public class BackgroundMapServiceImpl implements BackgroundMapService {

    /**
     * 所有的地图对象
     */
    private Map<String, BaseMapObject> mapObjects = new HashMap<>();

    private BackgroundTreeNode treeNode = new BackgroundTreeNode();

    @Override
    public List<String> findTaskRelationshipIdList(String mainMapId, String targetMapId) {
        return null;
    }

    @Override
    public List<BaseMapObject> findAllMapObjectList() {
        return new ArrayList<>(mapObjects.values());
    }

    @Override
    public BaseMapObject getMapObjectById(String mapId) {
        return mapObjects.getOrDefault(mapId, null);
    }

    @Override
    public void addMapInfo(String proMapId, String curMapId, String curMapName) {
        BackgroundTreeNode proBackgroundTreeNode = getBackgroundTreeNode(treeNode, proMapId);
        if (null == proBackgroundTreeNode) {
            proBackgroundTreeNode = treeNode;
        }
        BackgroundTreeNode create = new BackgroundTreeNode();
        create.setMapName(curMapName);
        create.setMapObjectId(curMapId);
        create.setProTreeNode(proBackgroundTreeNode);
        proBackgroundTreeNode.addBackgroundTreeNode(create);
    }


    public static void main(String[] arg0) {

        BackgroundTreeNode node0 = new BackgroundTreeNode();
        node0.setMapObjectId("0");
        node0.setMapName("node0");

        BackgroundTreeNode node11 = new BackgroundTreeNode();
        node11.setMapObjectId("11");
        node11.setMapName("node11");

        BackgroundTreeNode node111 = new BackgroundTreeNode();
        node111.setMapObjectId("111");
        node111.setMapName("node111");

        BackgroundTreeNode node1111 = new BackgroundTreeNode();
        node1111.setMapObjectId("1111");
        node1111.setMapName("node1111");

        BackgroundTreeNode node11111 = new BackgroundTreeNode();
        node11111.setMapObjectId("11111");
        node11111.setMapName("node11111");

        BackgroundTreeNode node11112 = new BackgroundTreeNode();
        node11112.setMapObjectId("11112");
        node11112.setMapName("node11112");

        BackgroundTreeNode node12 = new BackgroundTreeNode();
        node12.setMapObjectId("12");
        node12.setMapName("node12");

        BackgroundTreeNode node121 = new BackgroundTreeNode();
        node121.setMapObjectId("121");
        node121.setMapName("node121");

        BackgroundTreeNode node1211 = new BackgroundTreeNode();
        node1211.setMapObjectId("1211");
        node1211.setMapName("node1211");

        BackgroundTreeNode node13 = new BackgroundTreeNode();
        node13.setMapObjectId("13");
        node13.setMapName("node13");

        BackgroundTreeNode node131 = new BackgroundTreeNode();
        node131.setMapObjectId("131");
        node131.setMapName("node113");

        node1111.addBackgroundTreeNode(node11111);
        node1111.addBackgroundTreeNode(node11112);
        node111.addBackgroundTreeNode(node1111);
        node121.addBackgroundTreeNode(node1211);
        node11.addBackgroundTreeNode(node111);
        node12.addBackgroundTreeNode(node121);
        node13.addBackgroundTreeNode(node131);
        node0.addBackgroundTreeNode(node11);
        node0.addBackgroundTreeNode(node12);
        node0.addBackgroundTreeNode(node13);

        log.info("node0 = {}", node0);

//        List<String> ss = getBackgroundTreeNode(node0, "0", "11112");
//        List<String> ss2 = getBackgroundTreeNode(node0, "0", "1211");
//        log.info("ss = {}", JSON.toJSONString(ss));
//        log.info("ss2 = {}", JSON.toJSONString(ss2));
    }

    public BackgroundTreeNode getBackgroundTreeNode(BackgroundTreeNode sourNode, String mapId) {
        BackgroundTreeNode node = null;
        if (mapId.equals(sourNode.getMapObjectId())) {
            return sourNode;
        }
        for (BackgroundTreeNode sub : sourNode.getList()) {
            if (sub.getMapObjectId().equals(sourNode.getMapObjectId())) {
                return sub;
            } else {
                node = getBackgroundTreeNode(sub, mapId);
                if (null != node) {
                    return node;
                }
            }
        }
        return node;
    }

    public List<String> getMapIds(BackgroundTreeNode sourNode, String sourId, String targetId) {

        List<String> mapIdList = new ArrayList<>();
        for (int i = 0; i < sourNode.getList().size(); i++) {
            BackgroundTreeNode subNode = sourNode.getBackgroundTreeNode(i);
            if (subNode.getMapObjectId().equals(targetId)) {
                mapIdList.add(targetId);
                mapIdList.add(sourId);
            } else {
                List<String> subList = getMapIds(subNode, subNode.getMapObjectId(), targetId);
                if (subList != null && subList.size() > 0) {
                    mapIdList.addAll(subList);
                    mapIdList.add(sourId);
                }
            }
        }
        return mapIdList;
    }

}
